
#include "appXssl.h"

#define	TRAFFIC_STATISTICS_FLASH_ADDR  0x0805F800 /* Store gprs traffic statistics */
#define	DEV_CFG_FLASH_ADDR             0x0805F000 /* Store cfg informations (host cfg and etc..)*/
#define	DEV_INFO_FLASH_ADDR            0x0805E800 /* Store device informations (SN and etc...)*/
#define	DEV_LIC_FLASH_ADDR             0x0805E000 /* Store License ID Code */

unsigned int g_uiTrafficTimeTick = 0;
unsigned int g_uiMaxTraffic = 31457280; // 30M
extern unsigned int  g_uiTraffic;

xsUserCfg_t s_stUserCfg = {0};

void traffic_statistics_write(void)
{
  unsigned int FlashAddr;
  volatile FLASH_Status FLASHStatus;

  if(g_uiTrafficTimeTick < STATISTICS_INTERVAL){
    return;
  }
  
  g_uiTrafficTimeTick = 0;
  FLASH_Unlock();
  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); 

  FLASHStatus = FLASH_ErasePage(TRAFFIC_STATISTICS_FLASH_ADDR);
	while(RESET != FLASH_GetFlagStatus(FLASH_FLAG_BSY));

  FlashAddr = TRAFFIC_STATISTICS_FLASH_ADDR;
  FLASH_ProgramWord(FlashAddr, g_uiTraffic);
  FlashAddr += sizeof(unsigned int);
  FLASH_ProgramWord(FlashAddr, g_uiMaxTraffic);

  FLASH_Lock();
}

void traffic_statistics_read(void)
{
  unsigned int FlashAddr;

  FlashAddr = TRAFFIC_STATISTICS_FLASH_ADDR;
  g_uiTraffic = *(unsigned int *)FlashAddr;
  if(0xFFFFFFFF == g_uiTraffic){
    g_uiTraffic = 0;
  }
  
  FlashAddr += sizeof(unsigned int);
  g_uiMaxTraffic = *(unsigned int *)FlashAddr;
  if(0xFFFFFFFF == g_uiMaxTraffic){
    g_uiMaxTraffic = 31457280;
  }
  print_log(D_INFO, ("Traffic %d/%d (Bytes)\n", g_uiTraffic, g_uiMaxTraffic));
}

#if 1
const char *s_pcHost = "xmpp.l.google.com";
const char *s_pcMaster = "mqbest2000";
const char *s_pcSN = "xssl2000";
const char *s_pcPW = "n3tacc355";
const char *s_pcDomain = "gmail.com";
#else
const char *s_pcHost = "www.ghlsystems.com";
const char *s_pcMaster = "lomo@ghldbgsrv";
const char *s_pcDomain = "ghldbgsrv";
#endif

int user_cfg_read(void)
{
  unsigned int FlashAddr, uiValue, i, uiFlag = 1; /* 1: default cfg 0: flash cfg */
	
  FlashAddr = DEV_INFO_FLASH_ADDR;
  print_log(D_INFO, ("Read device information\n"));
  memset(&s_stUserCfg, 0, sizeof(xsUserCfg_t));
  for(i=0; i<sizeof(s_stUserCfg.acSn); ){
    uiValue = *(unsigned int *)(FlashAddr+i);
    if(0xFFFFFFFF == uiValue){
      goto _error;
    }
    if(0x00 == uiValue){
      break;
    }
    s_stUserCfg.acSn[i++] = uiValue & 0xFF;
    s_stUserCfg.acSn[i++] = uiValue>>8 & 0xFF;
    s_stUserCfg.acSn[i++] = uiValue>>16 & 0xFF;
    s_stUserCfg.acSn[i++] = uiValue>>24 & 0xFF;
  }
  FlashAddr += sizeof(s_stUserCfg.acSn);
  print_log(D_INFO, ("SN:%s\n", s_stUserCfg.acSn));
  
  for(i=0; i<sizeof(s_stUserCfg.acPwd); ){
    uiValue = *(unsigned int *)(FlashAddr+i);
    if(0xFFFFFFFF == uiValue){
      goto _error;
    }
    if(0x00 == uiValue){
      break;
    }
    s_stUserCfg.acPwd[i++] = uiValue & 0xFF;
    s_stUserCfg.acPwd[i++] = uiValue>>8 & 0xFF;
    s_stUserCfg.acPwd[i++] = uiValue>>16 & 0xFF;
    s_stUserCfg.acPwd[i++] = uiValue>>24 & 0xFF;
  }
  FlashAddr += sizeof(s_stUserCfg.acPwd);
  print_log(D_INFO, ("PW: ****%s\n", s_stUserCfg.acPwd+4));

  FlashAddr = DEV_CFG_FLASH_ADDR;
  s_stUserCfg.stCom.baudrate = *(unsigned int *)FlashAddr;
  if(0xFFFFFFFF == s_stUserCfg.stCom.baudrate){
    print_log(D_ERROR, ("Baudrate  is null\n"));
    goto _cfg;
  }
  FlashAddr += sizeof(unsigned int);
  
  uiValue = *(unsigned int *)FlashAddr;
  if(0xFFFFFFFF == uiValue){
    print_log(D_ERROR, ("COM attr is null\n"));
    goto _cfg;
  }else{
    s_stUserCfg.stCom.databit = (unsigned char)(uiValue>>24 & 0xFF);
    s_stUserCfg.stCom.stopbit = (unsigned char)(uiValue>>16 & 0xFF);
    s_stUserCfg.stCom.parity = (unsigned char)(uiValue>>8 & 0xFF);
    s_stUserCfg.stCom.flowctrl = (unsigned char)(uiValue & 0xFF);
  }
  FlashAddr += sizeof(unsigned int);

  for(i=0; i<sizeof(s_stUserCfg.acHost); ){
    uiValue = *(unsigned int *)(FlashAddr+i);
    if(0xFFFFFFFF == uiValue){
      print_log(D_ERROR, ("Host ip is null\n"));
      goto _cfg;
    }
    if(0x00 == uiValue){
      break;
    }
    s_stUserCfg.acHost[i++] = (unsigned char)(uiValue & 0xFF);
    s_stUserCfg.acHost[i++] = (unsigned char)(uiValue>>8 & 0xFF);
    s_stUserCfg.acHost[i++] = (unsigned char)(uiValue>>16 & 0xFF);
    s_stUserCfg.acHost[i++] = (unsigned char)(uiValue>>24 & 0xFF);
  }
  FlashAddr += sizeof(s_stUserCfg.acHost);

  s_stUserCfg.usPost = *(unsigned short *)FlashAddr;
  if(0xFFFF == s_stUserCfg.usPost){
    print_log(D_ERROR, ("Host post is null\n"));
    goto _cfg;
  }
  FlashAddr += sizeof(unsigned int);

  for(i=0; i<sizeof(s_stUserCfg.acDomain); ){
    uiValue = *(unsigned int *)(FlashAddr+i);
    if(0xFFFFFFFF == uiValue){
      print_log(D_ERROR, ("Domain name is null\n"));
      goto _cfg;
    }
    if(0x00 == uiValue){
      break;
    }
    s_stUserCfg.acDomain[i++] = (unsigned char)(uiValue & 0xFF);
    s_stUserCfg.acDomain[i++] = (unsigned char)(uiValue>>8 & 0xFF);
    s_stUserCfg.acDomain[i++] = (unsigned char)(uiValue>>16 & 0xFF);
    s_stUserCfg.acDomain[i++] = (unsigned char)(uiValue>>24 & 0xFF);
  }
  FlashAddr += sizeof(s_stUserCfg.acDomain);
  
  for(i=0; i<sizeof(s_stUserCfg.acMaster); ){
    uiValue = *(unsigned int *)(FlashAddr+i);
    if(0xFFFFFFFF == uiValue){
      print_log(D_ERROR, ("Master name is null\n"));
      goto _cfg;
    }
    if(0x00 == uiValue){
      break;
    }
    s_stUserCfg.acMaster[i++] = (unsigned char)(uiValue & 0xFF);
    s_stUserCfg.acMaster[i++] = (unsigned char)(uiValue>>8 & 0xFF);
    s_stUserCfg.acMaster[i++] = (unsigned char)(uiValue>>16 & 0xFF);
    s_stUserCfg.acMaster[i++] = (unsigned char)(uiValue>>24 & 0xFF);
  }
  FlashAddr += sizeof(s_stUserCfg.acMaster);
  if(0 != s_stUserCfg.acMaster[0]){
    sprintf(s_stUserCfg.acMasterJID, "%s@%s", s_stUserCfg.acMaster, s_stUserCfg.acDomain);
  }

  for(i=0; i<sizeof(s_stUserCfg.acDevPeer); ){
    uiValue = *(unsigned int *)(FlashAddr+i);
    if(0xFFFFFFFF == uiValue){
      print_log(D_ERROR, ("Device peer is null\n"));
      goto _cfg;
    }
    if(0x00 == uiValue){
      break;
    }
    s_stUserCfg.acDevPeer[i++] = (unsigned char)(uiValue & 0xFF);
    s_stUserCfg.acDevPeer[i++] = (unsigned char)(uiValue>>8 & 0xFF);
    s_stUserCfg.acDevPeer[i++] = (unsigned char)(uiValue>>16 & 0xFF);
    s_stUserCfg.acDevPeer[i++] = (unsigned char)(uiValue>>24 & 0xFF);
  }
  FlashAddr += sizeof(s_stUserCfg.acDevPeer);
  if(0 != s_stUserCfg.acDevPeer[0]){
    sprintf(s_stUserCfg.acDevJID, "%s@%s", s_stUserCfg.acDevPeer, s_stUserCfg.acDomain);
  }
  
  uiValue = *(unsigned int *)FlashAddr;
  s_stUserCfg.ucDataFormat = (unsigned char)(uiValue & 0xFF);

  uiFlag = 0;
  goto _cfg;
  
_error:
  print_log(D_ERROR, ("Get device info error\n"));
  return -1;
    
_cfg:
  if(1 == uiFlag){
    print_log(D_INFO, ("----------- Default cfg -----------\n"));
    s_stUserCfg.stCom.baudrate= 115200;
    s_stUserCfg.stCom.databit = 8;
    s_stUserCfg.stCom.stopbit = 1;
    s_stUserCfg.stCom.parity = 0;
    s_stUserCfg.stCom.flowctrl = 0;

    memcpy(s_stUserCfg.acHost, s_pcHost, strlen(s_pcHost));
    s_stUserCfg.usPost = 5222;
    memcpy(s_stUserCfg.acDomain, s_pcDomain, strlen(s_pcDomain));
    memcpy(s_stUserCfg.acMaster, s_pcMaster, strlen(s_pcMaster));
		sprintf(s_stUserCfg.acMasterJID, "%s@%s", s_stUserCfg.acMaster, s_stUserCfg.acDomain);
    s_stUserCfg.ucDataFormat = DATA_FORMAT_ASCII;
  }else{
    print_log(D_INFO, ("------ User cfg ------\n"));
  }
  print_log(D_INFO, ("Master  : %s\n", s_stUserCfg.acMasterJID));
  print_log(D_INFO, ("DevPeer : %s\n", s_stUserCfg.acDevJID));
  print_log(D_INFO, ("Baudrate: %d\n", s_stUserCfg.stCom.baudrate));
  print_log(D_INFO, ("Databit : %d\n", s_stUserCfg.stCom.databit));
  print_log(D_INFO, ("Stopbit : %d\n", s_stUserCfg.stCom.stopbit));
  print_log(D_INFO, ("Parity  : %d\n", s_stUserCfg.stCom.parity));
  print_log(D_INFO, ("Flowctrl: %d\n", s_stUserCfg.stCom.flowctrl));
  print_log(D_INFO, ("Host    : %s\n", s_stUserCfg.acHost));
  print_log(D_INFO, ("Post    : %d\n", s_stUserCfg.usPost));
  print_log(D_INFO, ("Domain  : %s\n", s_stUserCfg.acDomain));
  print_log(D_INFO, ("Master  : %s\n", s_stUserCfg.acMaster));
  print_log(D_INFO, ("Format  : %d\n", s_stUserCfg.ucDataFormat));
  print_log(D_INFO,  ("-----------------------\n"));
  return uiFlag;
}

int user_cfg_write(char cWFlag)
{
  int iRet = -1;
  volatile FLASH_Status FLASHStatus;
  unsigned int FlashAddr, uiValue, i;

  FLASH_Unlock();
  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); 

  if(WRITE_DEV_INFO == cWFlag){

    FlashAddr = DEV_INFO_FLASH_ADDR;
    
    FLASHStatus = FLASH_ErasePage(FlashAddr);
	  while(RESET != FLASH_GetFlagStatus(FLASH_FLAG_BSY));
    
    print_log(D_INFO, ("Record device info "));
    
    for(i=0; i<sizeof(s_stUserCfg.acSn); ){
      memcpy(&uiValue, s_stUserCfg.acSn+i, 4);
      FLASHStatus = FLASH_ProgramWord(FlashAddr+i, uiValue);
      if(FLASH_COMPLETE != FLASHStatus){
  			goto _exit;
  		}
      i += 4;
    }
    FlashAddr += sizeof(s_stUserCfg.acSn);
    for(i=0; i<sizeof(s_stUserCfg.acPwd); ){
      memcpy(&uiValue, s_stUserCfg.acPwd+i, 4);
      FLASHStatus = FLASH_ProgramWord(FlashAddr+i, uiValue);
      if(FLASH_COMPLETE != FLASHStatus){
  			goto _exit;
  		}
      i += 4;
    }
  }
  
  if(WRITE_CFG_INFO ==cWFlag){

    FlashAddr = DEV_CFG_FLASH_ADDR;
    
    FLASHStatus = FLASH_ErasePage(FlashAddr);
	  while(RESET != FLASH_GetFlagStatus(FLASH_FLAG_BSY));
    print_log(D_INFO, ("Record user cfg "));
    
    FLASHStatus = FLASH_ProgramWord(FlashAddr, s_stUserCfg.stCom.baudrate);
    if(FLASH_COMPLETE != FLASHStatus){
			goto _exit;
		}
    FlashAddr += sizeof(unsigned int);
    
    uiValue = (s_stUserCfg.stCom.databit<<24 | 
              s_stUserCfg.stCom.stopbit<<16 |
              s_stUserCfg.stCom.parity<<8   |
              s_stUserCfg.stCom.flowctrl);
    FLASH_ProgramWord(FlashAddr, uiValue);
    FlashAddr += sizeof(unsigned int);

    for(i=0; i<sizeof(s_stUserCfg.acHost); ){
      memcpy(&uiValue, s_stUserCfg.acHost+i, 4);
      FLASHStatus = FLASH_ProgramWord(FlashAddr+i, uiValue);
      if(FLASH_COMPLETE != FLASHStatus){
  			goto _exit;
  		}
      i += 4;
    }
    FlashAddr += i;

    FLASH_ProgramWord(FlashAddr, s_stUserCfg.usPost);
    FlashAddr += sizeof(unsigned int);

    for(i=0; i<sizeof(s_stUserCfg.acDomain); ){
      memcpy(&uiValue, s_stUserCfg.acDomain+i, 4);
      FLASHStatus = FLASH_ProgramWord(FlashAddr+i, uiValue);
      if(FLASH_COMPLETE != FLASHStatus){
  			goto _exit;
  		}
      i += 4;
    }
    FlashAddr += i;

    for(i=0; i<sizeof(s_stUserCfg.acMaster); ){
      memcpy(&uiValue, s_stUserCfg.acMaster+i, 4);
      FLASHStatus = FLASH_ProgramWord(FlashAddr+i, uiValue);
      if(FLASH_COMPLETE != FLASHStatus){
  			goto _exit;
  		}
      i += 4;
    }
    FlashAddr += i;

    for(i=0; i<sizeof(s_stUserCfg.acDevPeer); ){
      memcpy(&uiValue, s_stUserCfg.acDevPeer+i, 4);
      FLASHStatus = FLASH_ProgramWord(FlashAddr+i, uiValue);
      if(FLASH_COMPLETE != FLASHStatus){
  			goto _exit;
  		}
      i += 4;
    }
    FlashAddr += i;
    
    uiValue = (unsigned int)s_stUserCfg.ucDataFormat;
    FLASH_ProgramWord(FlashAddr, uiValue);
  }
  iRet = 0;
  
_exit:
  if(0 == iRet){
    print_log(D_INFO, (" OK\n"));
  }else{
    print_log(D_INFO, (" Fail\n"));
  }
  FLASH_Lock();
  return iRet;
}



